package com.itfreer.print.toPdf;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.stereotype.Component;

import com.itextpdf.text.DocumentException;
import com.itfreer.print.toPdf.ExcelToPdf;

/**
 * 基于ExcelToHtml先将excel转换为html，再基于HtmlToPdf将html转换为pdf
 */
@Component
public class ExcelToPdfImp implements ExcelToPdf {
	
	/**
	 * 将excel转换为pdf
	 * 
	 * @param excelFile word文件
	 * @param pdfFile html文件
	 * @throws IOException 
	 * @throws InvalidFormatException 
	 * @throws EncryptedDocumentException 
	 * @throws DocumentException 
	 */
	public void excelToPdf(InputStream excelFile, OutputStream pdfFile) 
			throws EncryptedDocumentException, InvalidFormatException, IOException, DocumentException {
		ByteArrayOutputStream htmlFile = new ByteArrayOutputStream();
		ExcelToHtmlImp toHtml = new ExcelToHtmlImp();
		toHtml.excelToHtml(excelFile, htmlFile);
		
		InputStream in = new ByteArrayInputStream(htmlFile.toByteArray());
		HtmlToPdfImp toPdf = new HtmlToPdfImp();
		toPdf.toPdf(in, pdfFile);
	}

}
